package servers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;

/**
 * Servlet implementation class HttpUrlConn
 */
@WebServlet("/jdkHttpConn")
public class HttpUrlConn extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HttpUrlConn() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
//		InputStream is = null;
//		BufferedReader br = null;
//		StringBuilder stringBuilder = new StringBuilder();
//		is = request.getInputStream();
//		br = new BufferedReader(new InputStreamReader(is));
//		char[] cs = new char[1024];
//        int len = 0;
//        while((len = br.read(cs, 0, cs.length)) != -1) {		  
//        	stringBuilder.append(cs, 0, len);   
//        }
		String content = request.getParameter("content");
        if(!"".equals(content) && null != content) {
        	response.getWriter().println(content);
        	//不需要解码
        	//response.getWriter().println(URLDecoder.decode(content, "UTF-8"));
            response.getWriter().println("这里是马航MH370，请指示，over");
        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		InputStream is = null;
		BufferedReader br = null;
		StringBuilder stringBuilder = new StringBuilder();
		is = request.getInputStream();
		br = new BufferedReader(new InputStreamReader(is));
		char[] cs = new char[1024];
        int len = 0;
        while((len = br.read(cs, 0, cs.length)) != -1) {		  
        	stringBuilder.append(cs, 0, len);   
        }
		User user = JSONArray.parseObject(stringBuilder.toString(), User.class);
		if("Anan".equals(user.getAccountNumber()) && "123456".equals(user.getPassowrd())) {
			response.getWriter().println(user.toString());
			response.getWriter().println("登陆成功！！！");
		}else {
			response.getWriter().println("账号或密码错误！！！");
		}
	}
}

class User {
	private String accountNumber;
	private String passowrd;
	public String getAccountNumber() {
		return accountNumber;
	}
	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}
	public String getPassowrd() {
		return passowrd;
	}
	public void setPassowrd(String passowrd) {
		this.passowrd = passowrd;
	}
	@Override
	public String toString() {
		return "User [accountNumber=" + accountNumber + ", passowrd=" + passowrd + "]";
	}
}
